package me.shurufa.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.HttpRequest;
import me.shurufa.R;
import me.shurufa.activities.BuyBookActivity;
import me.shurufa.bean.BookInfo;
import me.shurufa.model.NewBookInfo;
import me.shurufa.model.NewBookInfoResponse;
import me.shurufa.net.API;
import me.shurufa.net.RequestParams;
import me.shurufa.net.RequestServerTask;
import me.shurufa.net.okhttp.callback.HttpCallback;
import me.shurufa.response.BookInfoResp;
import me.shurufa.utils.Constants;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Utils;
import me.shurufa.widget.discoveryfab.ObservableScrollView;

/**
 * 作者 : andy
 * 日期 : 16/3/9 10:31
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 内容简介 Fragment
 */
public class BookInfoFragment extends BaseFragment implements View.OnClickListener {

    @Bind(R.id.book_img)
    ImageView book_img;

    @Bind(R.id.tv_author)
    TextView tv_author;

    @Bind(R.id.tv_publisher)
    TextView tv_publisher;

    @Bind(R.id.tv_subtitle)
    TextView tv_subtitle;

    @Bind(R.id.tv_year)
    TextView tv_year;

    @Bind(R.id.tv_page)
    TextView tv_page;

    @Bind(R.id.tv_price)
    TextView tv_price;

    @Bind(R.id.tv_type)
    TextView tv_type;

    @Bind(R.id.tv_series)
    TextView tv_series;

    @Bind(R.id.tv_isbn)
    TextView tv_isbn;

    @Bind(R.id.sv)
    ObservableScrollView mScrollView;

    @Bind(R.id.tv_content)
    TextView tv_content; // 内容简介

    @Bind(R.id.btn_buy)
    Button btn_buy;

    private BookInfo mBookInfo;
    private long bookId;

    public static BookInfoFragment newInstance(long bookId) {
        BookInfoFragment fragment = new BookInfoFragment();
        Bundle args = new Bundle();
        args.putLong(Constants.ARG_BOOK_ID, bookId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            bookId = getArguments().getLong(Constants.ARG_BOOK_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_info, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        mScrollView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        initData();
        initListener();
    }

    private void initListener() {
        btn_buy.setOnClickListener(this);
    }

    private void initData() {
        if (Utils.isFastClick(200)) {
            return;
        }

        RequestParams params = new RequestParams();
        params.addFormDataPart("id", bookId);
        HttpRequest.post(API.GET_BOOK_INFO, params, new HttpCallback<BookInfoResp>() {
            @Override
            public void onRespSuccess(BookInfoResp bookInfoResp) {
                try {
                    mBookInfo = bookInfoResp.data;
                    if (!TextUtils.isEmpty(mBookInfo.image)) {
                        Glide.with(getActivity()).load(mBookInfo.image).into(book_img);
                    }
                    tv_author.setText(getString(R.string.author) + (TextUtils.isEmpty(mBookInfo.author) ? "" : mBookInfo.author));
                    tv_publisher.setText(getString(R.string.publisher) + (TextUtils.isEmpty(mBookInfo.publisher) ? "" : mBookInfo.publisher));
                    tv_subtitle.setText(getString(R.string.subtitle) + (TextUtils.isEmpty(mBookInfo.subtitle) ? "" : mBookInfo.subtitle));
                    tv_year.setText(getString(R.string.publish_year) + (TextUtils.isEmpty(mBookInfo.pubdate) ? "" : mBookInfo.pubdate));
                    tv_page.setText(getString(R.string.page) + mBookInfo.pages);
                    tv_price.setText(getString(R.string.price) + (TextUtils.isEmpty(mBookInfo.price) ? "" : mBookInfo.price));
                    tv_type.setText(getString(R.string.book_type) + (TextUtils.isEmpty(mBookInfo.binding) ? "" : mBookInfo.binding));
                    tv_series.setText(getString(R.string.series) + (TextUtils.isEmpty(mBookInfo.series) ? "" : mBookInfo.series));
                    tv_series.setVisibility(TextUtils.isEmpty(mBookInfo.series) ? View.GONE : View.VISIBLE);
                    tv_content.setText(TextUtils.isEmpty(mBookInfo.summary) ? "" : mBookInfo.summary);

                    String isbn;
                    if (!TextUtils.isEmpty(mBookInfo.isbn13)) {
                        isbn = mBookInfo.isbn13;
                    } else if (!TextUtils.isEmpty(mBookInfo.isbn10)) {
                        isbn = mBookInfo.isbn10;
                    } else {
                        isbn = "";
                    }
                    tv_isbn.setText(getString(R.string.isbn) + isbn);


                    // 更新按钮
                    if (TextUtils.isEmpty(mBookInfo.buy_url)) {

                    }
                    btn_buy.setBackgroundResource(TextUtils.isEmpty(mBookInfo.buy_url) ? R.drawable.sel_btn_buy_disable : R.drawable.sel_btn_buy);
                    btn_buy.setText(TextUtils.isEmpty(mBookInfo.buy_url) ? R.string.not_found_buy_url : R.string.buy_this_book);
                } catch (Exception e) {
                }
            }

            @Override
            public void onRespFailure(BookInfoResp bookInfoResp) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 购买跳转到到网页
            case R.id.btn_buy:
                performClickBuyButton();

                break;
        }
    }

    public ObservableScrollView findScrollView() {
        return mScrollView;
    }

    /**
     * 购买按钮 点击进入
     */
    public void performClickBuyButton() {
        try {
            if (mBookInfo == null || mBookInfo.buy_url == null || TextUtils.isEmpty(mBookInfo.buy_url)) {
                Utils.showToast(getActivity().getString(R.string.not_found_buy_url));
                return;
            }
            Intent in = new Intent(getActivity(), BuyBookActivity.class);
            BuyBookActivity.initArguments(in, mBookInfo.buy_url);
            startActivity(in);
        } catch (Exception e) {
            Utils.showToast(R.string.not_found_buy_url);
        }
    }


}
